package com.family.pedigree.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.family.common.utils.StringUtils;
import com.family.pedigree.domain.FamilyCompany;
import com.family.pedigree.domain.FamilyCompanyAssets;
import com.family.pedigree.domain.FamilyUserInsurance;
import com.family.pedigree.domain.common.KeyValues;
import com.family.pedigree.mapper.FamilyCompanyAssetsMapper;
import com.family.pedigree.mapper.FamilyCompanyMapper;
import com.family.pedigree.service.IFamilyCompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 公司简介表 服务实现类
 * </p>
 *
 * @author forever
 * @since 2025-01-16
 */
@Service
@RequiredArgsConstructor
public class FamilyCompanyServiceImpl extends ServiceImpl<FamilyCompanyMapper, FamilyCompany> implements IFamilyCompanyService {

    private final FamilyCompanyAssetsMapper companyAssetsMapper;

    @Override
    public void add(FamilyCompany familyCompany) {
        save(familyCompany);
    }

    @Override
    public void edit(FamilyCompany familyCompany) {
        if (familyCompany.getId() == null)
            throw new RuntimeException("id不能为空");
        updateById(familyCompany);
    }

    @Override
    public List<FamilyCompany> getCompanyPage(FamilyCompany familyCompany) {
        List<FamilyCompany> companies = list(Wrappers.<FamilyCompany>lambdaQuery()
                .like(StringUtils.isNotBlank(familyCompany.getCompanyName()), FamilyCompany::getCompanyName, familyCompany.getCompanyName())
                .like(StringUtils.isNotBlank(familyCompany.getRegistrationNumber()), FamilyCompany::getRegistrationNumber, familyCompany.getRegistrationNumber())
                .like(StringUtils.isNotBlank(familyCompany.getCompanyType()), FamilyCompany::getCompanyType, familyCompany.getCompanyType())
                .like(StringUtils.isNotBlank(familyCompany.getLegalRepresentative()), FamilyCompany::getLegalRepresentative, familyCompany.getLegalRepresentative())
                .like(StringUtils.isNotBlank(familyCompany.getIndustry()), FamilyCompany::getIndustry, familyCompany.getIndustry())
                .like(StringUtils.isNotBlank(familyCompany.getBusinessType()), FamilyCompany::getBusinessType, familyCompany.getBusinessType())
                .like(StringUtils.isNotBlank(familyCompany.getCompanyProfile()), FamilyCompany::getCompanyProfile, familyCompany.getCompanyProfile())
                .like(StringUtils.isNotBlank(familyCompany.getLicence()), FamilyCompany::getLicence, familyCompany.getLicence())
                .like(StringUtils.isNotBlank(familyCompany.getAddress()), FamilyCompany::getAddress, familyCompany.getAddress())
                .like(StringUtils.isNotBlank(familyCompany.getContactNumber()), FamilyCompany::getContactNumber, familyCompany.getContactNumber())
                .orderByAsc(FamilyCompany::getCreateTime));
        if (CollUtil.isNotEmpty(companies)) {
            companies.forEach(company -> {
                List<FamilyCompanyAssets> assets = companyAssetsMapper.selectList(Wrappers.<FamilyCompanyAssets>lambdaQuery().eq(FamilyCompanyAssets::getCompanyId, company.getId()));
                company.setAssetsList(assets);
            });
        }
        return companies;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void delById(Long id) {
        removeById(id);
        companyAssetsMapper.delete(Wrappers.<FamilyCompanyAssets>lambdaQuery().eq(FamilyCompanyAssets::getCompanyId, id));
    }

    @Override
    public List<KeyValues> findAmountByYear() {
        List<KeyValues> list = CollUtil.newArrayList();
        for (int i = 0; i < 5; i++) {
            int year = 2025 - i;
            KeyValues vo = new KeyValues();
            vo.setKeyA(year + "");
            LocalDate now = LocalDate.of(year, 1, 1);
            LocalDate before = LocalDate.of(year + 1, 1, 1);
            List<FamilyCompanyAssets> result = companyAssetsMapper.selectList(Wrappers.<FamilyCompanyAssets>lambdaQuery()
                    .ge(FamilyCompanyAssets::getParticularYear, now)
                    .lt(FamilyCompanyAssets::getParticularYear, before));

            if (CollUtil.isNotEmpty(result)) {
                BigDecimal total = result.stream().map(FamilyCompanyAssets::getAnnualAssets).filter(Objects::nonNull).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
                vo.setValueA(total);
            }
            list.add(vo);

        }
        list.sort(Comparator.comparing(KeyValues::getKeyA));
        return list;
    }

    @Override
    public List<KeyValues> findTypeAmount() {
        return companyAssetsMapper.findTypeAmount();
    }
}
